package wx

import (
	"encoding/json"
	"fmt"
	"jsjb/tools/curl"
)

type Wechat struct {
	appid, secret string
}
type accessToken struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
}

func New(appid, secret string) *Wechat {
	w := &Wechat{
		appid:  appid,
		secret: secret,
	}
	return w
}

// GetUserPhoneNumber 获取用户信息
func (w *Wechat) GetUserPhoneNumber() {
	abc := w.getAccessToken()
	url := fmt.Sprintf("/wxa/business/getuserphonenumber?access_token=%s", abc.AccessToken)
	data, _ := json.Marshal(struct {
		code string
	}{code: "12121"})
	res := curl.New(url, "https://api.weixin.qq.com", map[string]string{}).PostJson(string(data))
	fmt.Println(string(res))

}

//获取accession
func (w *Wechat) getAccessToken() accessToken {
	var a accessToken
	url := fmt.Sprintf("/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", w.appid, w.secret)
	res := curl.New(url, "https://api.weixin.qq.com", map[string]string{"zxx": "123"}).Get()
	json.Unmarshal(res, &a)
	return a
}
